package core.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.example.heizhu.appfuncore.R;

import java.util.ArrayList;
import java.util.List;

import core.android.deeplinks.deeplinking.AKDeeplink;
import core.android.deeplinks.deeplinking.annotations.DeeplinkQueryParameter;
import core.android.deeplinks.deeplinking.annotations.DeeplinkRoute;
import core.android.deeplinks.deeplinking.annotations.DeeplinkRouteParameter;
import core.android.deeplinks.model.IntentRouteImpl;
import core.app.fragment.CategoryFragment;
import core.app.fragment.HotFragment;
import core.app.fragment.IndexFragment;
import core.app.fragment.MineFragment;
import core.app.fragment.NeiborFragment;


@DeeplinkRoute("main/:fragemnt_index")
public class MainFragmentActivity extends AppCompatActivity {
    @DeeplinkRouteParameter("fragemnt_index")
    int fragemnt_index = 0;
    @DeeplinkQueryParameter("referrer")
    String mReferrer;

    ArrayList<Fragment> fragmentList = new ArrayList<>();
    int currentIndex = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fragment);
        AKDeeplink.INSTANCE.inject(this);
        initFragmentList();
        setCurrentFragment(fragemnt_index);
    }

    private void initFragmentList() {
        fragmentList.add(new IndexFragment());
        fragmentList.add(new HotFragment());
        fragmentList.add(new NeiborFragment());
        fragmentList.add(new CategoryFragment());
        fragmentList.add(new MineFragment());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Bundle routeParametersBundle = intent.getBundleExtra(IntentRouteImpl.ROUTE_PARAMETERS_BUNDLE_KEY);
        try {
            fragemnt_index = Integer.parseInt(routeParametersBundle.getString("fragemnt_index"));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        setCurrentFragment(fragemnt_index);
    }

    private void setCurrentFragment(int index) {
//        KLog.d("currentIndex="+currentIndex+"  index="+index);
        if (index == currentIndex) {
//            KLog.d("不需要重复添加fragment");
            return;
        }

        Fragment currentFragment = getVisibleFragment();
        FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
        if (currentFragment != null) {
            trx.hide(currentFragment);
        }
        currentFragment = fragmentList.get(index);
        if (!currentFragment.isAdded()) {
            trx.add(R.id.frame_layout, currentFragment);
        }
        trx.show(currentFragment).commit();
        currentIndex = index;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        Fragment visibleFragment = getVisibleFragment();
        Log.d(getLocalClassName(), "fragment=" + visibleFragment);
        if (visibleFragment == null) {
            super.onBackPressed();
        } else {
            visibleFragment.onOptionsItemSelected(null);
        }
    }

    //当前显示的fragment 只适用于v4包
    public Fragment getVisibleFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        List<Fragment> fragments = fragmentManager.getFragments();
//        KLog.d(fragments);
        if (fragments != null) {
            for (int i = fragments.size() - 1; i >= 0; i--) {
                Fragment fragment = fragments.get(i);
                if (fragment != null && fragment.isVisible())
                    return fragment;
            }
        }
        return null;
    }
}
